<template>
  <div class="login">
    <div class="loginbox">
      <p>千峰2213后台管理系统</p>

      <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="60px" class="demo-ruleForm">
        <el-form-item label="账号" prop="adminname">
          <el-input v-model="ruleForm.adminname" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from "@/api/http ";

import { mapMutations } from "vuex";

console.log(2, loginApi);

export default {
  name: "",
  data() {
    // ruleFormRef=null;

    return {
      ruleFormRef: null,
      ruleForm: {
        adminname: "",
        password: "",
      },
      rules: {
        adminname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapMutations(["updataUsername", "updataRole", "updatacheckedkeys"]),
    //提交表单
    submitForm(formEl) {
      if (!formEl) return;
      formEl.validate((valid) => {
        if (valid) {
          console.log("submit!");
          console.log(this.resetForm);
          loginApi(this.ruleForm).then((res) => {
            console.log(res);
            let { adminname, checkedkeys, role, token } = res.data.data;
            if (res.data.code == 200) {
              // this.$router.push('/home')
              //01.将用户名信息存在vuex中
              this.updataUsername(adminname);
              //02.将左边菜单栏信息也存在vuex中
              this.updatacheckedkeys(checkedkeys);
              //03.将角色role也存在vuex中
              this.updataRole(role);
              //04.将 token 存到 vuex中
              localStorage.setItem("token", token);
              //05 跳转到首页
              this.$router.push("/home");
            }
          });
        } else {
          console.log("error submit!");
          return false;
        }
      });
    },
    resetForm(formEl) {
      if (!formEl) return;
      formEl.resetFields();
    },
  },
  mounted() {
    console.log('refrefrefrefref', this.$refs.ruleFormRef);
    this.ruleFormRef = this.$refs.ruleFormRef;
  },
};
</script>
<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
}

.login {
  width: 100%;
  height: 100%;
  background-image: url(@/assets/img/jjjj.jpg);
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .loginbox {
    width: 400px;
    height: 300px;
    background-color: #fff;

    >p {
      width: 100%;
      height: 60px;
      background-color: red;
      line-height: 60px;
      text-align: center;
      color: #fff;
    }
  }

  .el-form {
    padding: 30px;
    box-sizing: border-box;
  }

  .el-form-item {
    margin-bottom: 20px;
  }

  .el-button {
    width: 80px;
  }
}
</style>